/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-10-15 16:34:42
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-15 16:37:42
 * @FilePath: /react18-vite-admin/src/service/biz/department/useDepartmentTree.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */



import { useState } from 'react';

import { readDeptTree } from '@/api/department';
import type { DeptTreeItem } from '@/typings/api/department';
import { useStateRef } from '@/service/useStateRef';

export const useDepartmentTree = () => {
    const [tree, setTree] = useState<DeptTreeItem[]>([]);
    const [loading, loadingRef, setLoading] = useStateRef(false);

    const getTree = () => {
        if (loadingRef.current) return;
        setLoading(true);
        return readDeptTree()
            .then((res) => {
                setTree(res.data);
                return res;
            })
            .finally(() => {
                setLoading(false);
            });
    };

    return {
        tree,
        loading,
        getTree
    };
}
